﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using pwblog.Entities;
using pwblog.BusinessObject;
using System.IO;
using pwblog.Common;

namespace pwblog.Web
{
    public partial class EditArticle : System.Web.UI.Page
    {
        private string idArticle;

        protected void Page_Load(object sender, EventArgs e)
        {
            idArticle = Request.QueryString["Id"];
            if (!Page.IsPostBack)
            {
                if (idArticle != null)
                {
                    LoadDataArticle(idArticle);
                }
            }
        }

        private void LoadDataArticle(string id)
        {
            try
            {
                Article article = new ArticleBo().GetArticleById(Int32.Parse(id));

                // load title
                tbNameArticle.Text = article.Name;

                // load image
                IThumb.ImageUrl = article.Image;

                // load category
                var categories = new CategoryBo().GetListItemCategories();
                if (categories.Length > 0)
                {
                    ddlCategory.Items.AddRange(categories);
                }
                ddlCategory.SelectedValue = article.IdCategory.ToString();

                // load description article
                tbDesciption.Text = article.Description;

                // load contents article
                string path = Server.MapPath(@"\Web\Articles\" + article.Id + ".txt");
                if (File.Exists(path))
                {
                    using (StreamReader reader = new StreamReader(path))
                    {
                        ckEditorArticle.Text = reader.ReadToEnd();
                    }
                }

            }
            catch
            { }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            string nameArticle = tbNameArticle.Text;
            string image = IThumb.ImageUrl;
            int idCategory = Int32.Parse(ddlCategory.SelectedValue);
            string contents = ckEditorArticle.Text;
            string description = tbDesciption.Text;
            if (String.IsNullOrEmpty(description))
            {
                description = RegexUtility.stripHtmlTagsUsingHAP(contents).Substring(0, 50);
            }
            if (new ArticleBo().UpdateArticle2(idArticle, nameArticle, idCategory, description, image))
            {
                // thanh cong
                string path = Server.MapPath(@"\Web\Articles\" + idArticle + ".txt");
                if (File.Exists(path))
                {
                    using (StreamWriter writer = new StreamWriter(path))
                    {
                        writer.WriteLine(ckEditorArticle.Text);
                    }
                }

                Response.Redirect(@"~/Web/DetailsArticle.aspx?id=" + idArticle);
            }
            else
            {
                // that bai
            }
        }

        protected void btnUpImage_Click(object sender, EventArgs e)
        {
            try
            {
                if (fuThumbs.HasFile)
                {

                    if (CheckFileType(fuThumbs.FileName))
                    {
                        string upFolder = Server.MapPath("~/Web/Articles/thumbs");
                        if (Directory.Exists(upFolder))
                        {
                            string filePath = upFolder + "\\" + fuThumbs.FileName;
                            fuThumbs.SaveAs(filePath);
                            IThumb.ImageUrl = "/Web/Articles/thumbs/" + fuThumbs.FileName;

                            try
                            {
                                FileStream fs = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read);
                                System.Drawing.Image image = System.Drawing.Image.FromStream(fs);
                                int fileLength = Convert.ToInt32(fs.Length);
                                lbImageLength.Text = fileLength.ToString();
                                int fileWidth = image.Width;
                                lbImageWidth.Text = fileWidth.ToString();
                                int fileHeight = image.Height;
                                lbImageHeight.Text = fileHeight.ToString();
                            }
                            catch (Exception ex)
                            {
                                Console.WriteLine(ex.Message);
                            }

                        }
                    }
                }
            }
            catch { return; }
        }

        private bool CheckFileType(string fileName)
        {

            string ext = Path.GetExtension(fileName);

            switch (ext.ToLower())
            {

                case ".gif":

                    return true;

                case ".png":

                    return true;

                case ".jpg":

                    return true;

                case ".jpeg":

                    return true;

                default:

                    return false;

            }

        }
    }
}